/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.resources.nfs;

import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;

import fr.jade.base.JadeBaseComponent;
import fr.jade.fraclite.api.control.NoSuchAttributeException;
import fr.jade.resources.api.NfsConnector;
import fr.jade.util.FractalUtil;
import fr.jade.util.JadeException;
import fr.jade.util.ShellCommand;

public class ClientWrapper extends JadeBaseComponent implements LifeCycleController {

  // Attributes
  protected static final String mountAttName = "mount";
  protected String mount;
  
  protected static final String[] listAtt = {mountAttName};
  
  // Bindings
  protected static final String nfsConnectorItfName = "nfs-connector";
  protected NfsConnector nfsConnector;
  
  protected static final String[] listItf = {nfsConnectorItfName};
  
  // local attributes
  boolean started = false;
  
  public ClientWrapper(){
    
  }
  
  @Override
  public String getFcState() {
    return started?STARTED:STOPPED;
  }

  @Override
  public void startFc() throws IllegalLifeCycleException {
    if(!started){
      if(nfsConnector.syncStart(FractalUtil.getLifeCycleController(self))){
        started = true;
        String mountCmd = "mount -t nfs "+nfsConnector.getHost()+":"+nfsConnector.getExport()+" "+mount+" -o soft";
        log(mountCmd);
        try {
          ShellCommand.syncExec(mountCmd, new String[0]);
        } catch (JadeException e) {
          e.printStackTrace();
        }
      }
    }
  }

  @Override
  public void stopFc() throws IllegalLifeCycleException {
    if(started){
      started = false;
      String umountCmd = "umount "+mount+" -fl";
      log(umountCmd);
      try {
        ShellCommand.syncExec(umountCmd, new String[0]);
      } catch (JadeException e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  public void bindFc(String itfName, Object itfValue) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if(itfName.equals(nfsConnectorItfName)){
      nfsConnector = (NfsConnector)itfValue;
    }else{
      super.bindFc(itfName, itfValue);
    }
  }

  @Override
  public void unbindFc(String itfName) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if(itfName.equals(nfsConnectorItfName)){
      nfsConnector = null;
    }else{
      super.unbindFc(itfName);
    }
  }
  
  @Override
  public Object lookupFc(String itfName) throws NoSuchInterfaceException {
    if(itfName.equals(nfsConnectorItfName)){
      return nfsConnector;
    }else{
      return super.lookupFc(itfName);
    }
  }
  
  @Override
  public String[] listFc() {
    return super.concat(listItf, super.listFc());
  }

  @Override
  public String getAttribute(String name) throws NoSuchAttributeException {
    if(name.equals(mountAttName)){
      return mount;
    }else{
      return super.getAttribute(name);
    }
  }
  
  @Override
  public String[] listFcAtt() {
    return super.concat(listAtt, super.listFcAtt());
  }

  @Override
  public void setAttribute(String name, String value) throws NoSuchAttributeException {
    if(name.equals(mountAttName)){
      mount = value;
    }else{
      super.setAttribute(name, value);
    }
  }

}
